<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>asyncAwait</title>
</head>
<body>
<script>
    /**
     * async 函数必定返回Promise
     * await 返回异步函数的resolve的值
     * @returns {Promise<string>}
     */
    async function f1() {
        return 'f1';
    }

    async function f2() {
        return Promise.resolve('f2');
    }

    console.log(f1()); // Promise
    console.log(f2()); // Promise

    async function f3() {
        let p1 = Promise.resolve('p1');
        // p1.then(r => {
        //     console.log(r)
        // });
        let r1 = await p1; // 等价于上面then()的写法
        console.log(r1);
    }

    f3();  // p1

    async function f4() {
        let r2 = await 'f4';
        console.log(r2);
    }

    f4();  // f4

    async function f5() {
        let r5 = await f1();
        console.log(r5);
    }

    f5(); // f1

    async function f6() {
        let p6 = Promise.reject('f6');
        try {
            let r6 = await p6;
            console.log(r6);
        } catch (e) {
            console.warn(e); // 异常信息 f6
        }

    }
    f6();


</script>
</body>
</html>